/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Controladores.ControladorCompeticion;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Abdelilah
 */
public class InterfazJornada extends javax.swing.JPanel {

    public static InterfazJornada Instancia = null;
    private InterfazPartido interfaz_partido= null;

    private Boolean es_gestor;
    
   public static InterfazJornada getInstancia() {
         if (Instancia==null){
            Instancia = new InterfazJornada();
        }
        return Instancia;
    }
        
    /**
     * Creates new form InterfazJornada
     */
    public InterfazJornada() {
        initComponents();
        jPanel2.setLayout(new BorderLayout() );
        jPanel2.add(InterfazPartido.getInstancia(), BorderLayout.CENTER);
        javax.swing.JPanel Jpanel_Cancelar2 = new javax.swing.JPanel();
        jPanel2.add(Jpanel_Cancelar2, BorderLayout.SOUTH);
        Jpanel_Cancelar2.setLayout(new BorderLayout() );
    }
    
    public void mostrarJornada(Integer numJornada){
        jTextNumJornada.setText(numJornada.toString());
    }

    public String[] cargarDatosFormulario() {
        String [] Datos = new String[1];
        Datos[0] = this.jTextNumJornada.getText();
        
        return Datos;
    }
    public void add_Jornada_fila(int fila, String numJornada){
        DefaultTableModel temp = (DefaultTableModel) jTable1.getModel();
        if(fila==jTable1.getRowCount()){
            Object nuevo[] = {fila+1, ""};
            temp.addRow(nuevo);
        }
        this.jTable1.setValueAt(numJornada, fila, 0);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton_CrearJornada = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextNumJornada = new javax.swing.JTextField();
        jButton_ConsultarJornada = new javax.swing.JButton();
        jButton_ConsultarPartido = new javax.swing.JButton();
        jButton_CrearPartido = new javax.swing.JButton();
        jButton_ModificarJornada = new javax.swing.JButton();
        jButton_EliminarJornada = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton_CrearJornada.setText("Crear");
        jButton_CrearJornada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CrearJornadaActionPerformed(evt);
            }
        });

        jLabel2.setText("Numero Jornada");

        jButton_ConsultarJornada.setText("Consultar");
        jButton_ConsultarJornada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ConsultarJornadaActionPerformed(evt);
            }
        });

        jButton_ConsultarPartido.setText("Consultar Partido");
        jButton_ConsultarPartido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ConsultarPartidoActionPerformed(evt);
            }
        });

        jButton_CrearPartido.setText("Crear Partido");
        jButton_CrearPartido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CrearPartidoActionPerformed(evt);
            }
        });

        jButton_ModificarJornada.setText("Modificar");
        jButton_ModificarJornada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ModificarJornadaActionPerformed(evt);
            }
        });

        jButton_EliminarJornada.setText("Eliminar");
        jButton_EliminarJornada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_EliminarJornadaActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numero Jornada"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton_CrearJornada)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextNumJornada, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton_ConsultarPartido, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton_CrearPartido, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(56, 56, 56))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton_ConsultarJornada, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton_ModificarJornada, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton_EliminarJornada, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(302, Short.MAX_VALUE))))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton_ConsultarPartido, jButton_CrearPartido});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton_ConsultarJornada, jButton_CrearJornada, jButton_EliminarJornada, jButton_ModificarJornada});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, jTextNumJornada});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextNumJornada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton_ConsultarPartido, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton_CrearPartido)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_CrearJornada)
                    .addComponent(jButton_ConsultarJornada)
                    .addComponent(jButton_ModificarJornada)
                    .addComponent(jButton_EliminarJornada, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton_ConsultarPartido, jButton_CrearPartido});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton_ConsultarJornada, jButton_CrearJornada, jButton_EliminarJornada, jButton_ModificarJornada});

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 686, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jPanel1, jPanel2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jPanel1, jPanel2});

    }// </editor-fold>//GEN-END:initComponents

    private void jButton_ConsultarJornadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ConsultarJornadaActionPerformed
        
       if(ControladorCompeticion.getInstancia().consultarJornada(cargarDatosFormulario())) {
            jPanel1.setVisible(true);
            jPanel2.setVisible(false);      
            this.jButton_ConsultarPartido.setEnabled(true);
            if(this.es_gestor) {
                this.jButton_CrearPartido.setEnabled(true);
            }else {
                this.jButton_CrearPartido.setEnabled(false);
            }
            this.jButton_CrearJornada.setEnabled(false);
            this.jButton_ConsultarJornada.setEnabled(false);
            this.jButton_ModificarJornada.setEnabled(true);
            this.jButton_EliminarJornada.setEnabled(true);
       }else {
            this.mostrarMensaje("JORNADA NO ENCONTRADA", "ERROR", 0);
        }
    }//GEN-LAST:event_jButton_ConsultarJornadaActionPerformed

    private void jButton_ConsultarPartidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ConsultarPartidoActionPerformed
        
        jPanel1.setVisible(false);
        if (interfaz_partido==null){
            interfaz_partido=InterfazPartido.getInstancia();
            jPanel2.add(interfaz_partido);
        }
        interfaz_partido.inicializar_Consultar(es_gestor);
        interfaz_partido.setVisible(true);    
        jPanel2.setVisible(true);
        
    }//GEN-LAST:event_jButton_ConsultarPartidoActionPerformed

    private void jButton_CrearPartidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CrearPartidoActionPerformed
        
        jPanel1.setVisible(false);
        if (interfaz_partido==null){
            interfaz_partido=InterfazPartido.getInstancia();
            jPanel2.add(interfaz_partido);
        }
        interfaz_partido.inicializar_Crear();
        interfaz_partido.setVisible(true);    
        jPanel2.setVisible(true);

    }//GEN-LAST:event_jButton_CrearPartidoActionPerformed

    private void jButton_CrearJornadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CrearJornadaActionPerformed
       if(! ControladorCompeticion.getInstancia().consultarJornada(this.cargarDatosFormulario())) {
            if(ControladorCompeticion.getInstancia().addJornada(this.cargarDatosFormulario())) {
            limpiarInterfaz();
        }
       }else{
            mostrarMensaje("EXISTE UNA JORNADA CON LOS MISMOS DATOS", "AVISO", 0);   
       }
       
    }//GEN-LAST:event_jButton_CrearJornadaActionPerformed

    private void jButton_ModificarJornadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ModificarJornadaActionPerformed
        if(ControladorCompeticion.getInstancia().modificarJornada(this.cargarDatosFormulario())) {
            limpiarInterfaz();
        }
    }//GEN-LAST:event_jButton_ModificarJornadaActionPerformed

    private void jButton_EliminarJornadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_EliminarJornadaActionPerformed
        int n=JOptionPane.showConfirmDialog(null, "¿Seguro que quieres eliminar la jornada?",
                "Atención", JOptionPane.YES_NO_OPTION);
        if (n==JOptionPane.YES_OPTION) {
            if(ControladorCompeticion.getInstancia().eliminarJornada()) {
                limpiarInterfaz();
            }
        }
    }//GEN-LAST:event_jButton_EliminarJornadaActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int row_index = jTable1.getSelectedRow();
        
        mostrarJornada(Integer.parseInt(jTable1.getValueAt(row_index, 0).toString()));
                            
        ControladorCompeticion.getInstancia().actualizarJornadaActiva(row_index);
    }//GEN-LAST:event_jTable1MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_ConsultarJornada;
    private javax.swing.JButton jButton_ConsultarPartido;
    private javax.swing.JButton jButton_CrearJornada;
    private javax.swing.JButton jButton_CrearPartido;
    private javax.swing.JButton jButton_EliminarJornada;
    private javax.swing.JButton jButton_ModificarJornada;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextNumJornada;
    // End of variables declaration//GEN-END:variables

    void inicializar_Crear() {
        
        this.es_gestor = true;
        jPanel1.setVisible(true);       
        jPanel2.setVisible(false);   
        this.jButton_ConsultarPartido.setEnabled(false);
        this.jButton_CrearPartido.setEnabled(false);
        this.jButton_CrearJornada.setEnabled(true);
        this.jButton_ConsultarJornada.setEnabled(false);
        this.jButton_ModificarJornada.setEnabled(false);
        this.jButton_EliminarJornada.setEnabled(false);
        limpiarInterfaz();
    }

    void inicializar_Consultar(boolean esgestor) {
        
        this.es_gestor = esgestor;
        jPanel1.setVisible(true);
        jPanel2.setVisible(false);   
        this.jButton_ConsultarPartido.setEnabled(false);
        this.jButton_CrearPartido.setEnabled(false);
        this.jButton_CrearJornada.setEnabled(false);
        this.jButton_ConsultarJornada.setEnabled(true);
        this.jButton_ModificarJornada.setEnabled(false);
        this.jButton_EliminarJornada.setEnabled(false);
        limpiarInterfaz();

    }

        public void mostrarMensaje(String mensaje, String titulo, int i) {
        JOptionPane.showMessageDialog(null, mensaje, titulo, i);
    }
        
        public void limpiarInterfaz(){
            jTextNumJornada.setText("");
            limpiarTabla();
        }
        
       public void limpiarTabla(){
 
        DefaultTableModel temp = (DefaultTableModel) jTable1.getModel();
        for (int i = temp.getRowCount() - 1; i >= 0; i--) {
            temp.removeRow(i);
        }
        jTable1.clearSelection();
    }
}
